package com.runa.monitor.platinterface.dispatch.conn;

import com.runa.monitor.platinterface.dispatch.grpcComm.*;
import com.runa.persistence.repository.CommNetServiceRepository;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Order(888)
@Service
public class Client implements CommandLineRunner {

    COMMDispatchGrpc.COMMDispatchStub commDispatchStub;

    @Value("${dispatch.grpc.server.ip:}")
    private String serverIp;
    @Value("${dispatch.grpc.server.port:}")
    private Integer serverPort;
    @Value("${dispatch.grpc.local.node_name:comm}")
    private String nodeName;
    @Value("${dispatch.grpc.local.ip:}")
    private String localIp;
    @Value("${grpc.port}")
    private Integer localPort;

//    @Resource
//    private CommNetServiceRepository commNetServiceRepository;

    @Override
    public void run(String... args) throws Exception {
//        List<CommNetService> all = commNetServiceRepository.findAll();
//        System.out.println(all);
//        CommNetService commNetService = newComm CommNetService();
//        commNetService.setName("ff");
//        commNetService.setPort("34325");
//        commNetService.setProtocol("fwfff");
//        commNetService.setEnable(1);
////        commNetService.setType(NetServiceTypeEnum.ActiveReport);
//        commNetService.setType(NetServiceTypeEnum.主动上报);
//        commNetServiceRepository.save(commNetService);

        commDispatchStub = COMMDispatchGrpc.newStub(NettyChannelBuilder
                .forAddress(serverIp, serverPort)
                .negotiationType(NegotiationType.PLAINTEXT)
                .build()).withMaxInboundMessageSize(Integer.MAX_VALUE).withMaxOutboundMessageSize(Integer.MAX_VALUE);

//        HelloRequest request = HelloRequest.newBuilder().setName("fffffffff").build();
//        HelloReply response;
//        commDispatchBlockingStub.send(request,null);
        System.out.println();
        login();
        log.info("连接注册完成----------");
    }

    private void login() {
        log.info("comm开始注册... localIp: {} localPort: {}", localIp, localPort);
        commDispatchStub.login(NodeComm.newBuilder().setName(nodeName).setAddr(localIp + ":" + localPort).build(), new LoginStreamResponse(this));
    }


    void delayLogin() {
        try {
            TimeUnit.SECONDS.sleep(5);
            login();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public void chatFlowEntryUnit() {
        FlowUnitCommStreamResponse streamResponse = new FlowUnitCommStreamResponse(this);
        StreamObserver<FlowUnitComm> streamObserver = commDispatchStub.interactionFlow(streamResponse);
        streamObserver.onNext(FlowUnitComm.newBuilder().build());
    }
}
